package com.baomidou.ant.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.AllMachine;
import com.baomidou.ant.entity.SysUser;
import com.baomidou.ant.service.IAllMachineService;
import com.baomidou.ant.service.IMachineInfoService;
import com.baomidou.ant.service.ISysUserService;
import com.baomidou.ant.vo.request.*;
import com.baomidou.ant.vo.response.ResponseListMachineVo;
import com.baomidou.ant.vo.response.ResponseMachineNameVo;
import com.baomidou.ant.vo.response.ResponseMachineNumVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@RestController
@RequestMapping("/all-machine")
public class AllMachineController {
    @Autowired
    private IAllMachineService allMachineService;
    //新增机器接口
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IMachineInfoService machineInfoService;

    //展示机器选项
    @PostMapping("/listMachine")
    public Result listMachine(@RequestBody RequestDtoVo requestDtoVo){
        List<ResponseMachineNameVo> responseMachineName=allMachineService.getMachine(requestDtoVo);
        return Result.getSuccess().setData(responseMachineName);
    }

    @PostMapping("/listMachineNum")
    public Result listMachineNum(@RequestBody RequestDtoVo requestDtoVo){
        List<ResponseMachineNumVo> responseMachineNum=allMachineService.getMachineNum(requestDtoVo);
        return Result.getSuccess().setData(responseMachineNum);
    }

    //检录机器展示
//    @PostMapping("/listMachineOfCheckIn")
//    public Result listMachineOfCheckIn(@RequestBody RequestDtoVo requestDtoVo){
//
//    }


    //机器管理展示机器
    @PostMapping("/list")
    public Result list(@RequestBody RequestListMachineVo requestListMachineVo){
        IPage<ResponseListMachineVo> machineInfo=allMachineService.listMachine(requestListMachineVo);
        return Result.getSuccess().setData(machineInfo);
    }

    //机器信息回显接口
    @GetMapping("/getMachine/{id}")
    public Result getMachine(@PathVariable Integer id){
        AllMachine machine=allMachineService.getById(id);
        return Result.getSuccess().setData(machine);
    }

    //删除机器接口
    @PostMapping("/delete")
    public Result delete(@RequestBody RequestDeleteVo requestDeleteVo){
        SysUser user=userService.getById(requestDeleteVo.getUserId());
        //如果不是管理员
        if (!Objects.equals(user.getType(), "1")){
            return Result.getFailure().setMsg("您不是管理员，无权删除机器");
        }
        //如果是管理员
        if(allMachineService.removeById(requestDeleteVo.getGoalId())){
            return Result.getSuccess().setMsg("删除成功");
        }else{
            return Result.getFailure().setMsg("删除失败");
        }
    }

    //编辑接口
    @PostMapping("/update")
    public Result update(@RequestBody RequestUpdateMachineVo requestUpdateMachineVo){
        AllMachine machine=allMachineService.getById(requestUpdateMachineVo.getId());
        if(!Objects.equals(requestUpdateMachineVo.getMachineNum(),machine.getMachineNum())&&
                allMachineService.queryByNum(requestUpdateMachineVo.getMachineNum(),machine.getMachineInfoId())){
            return Result.getFailure().setMsg("保存失败,存在相同编号");
        }
        BeanUtils.copyProperties(requestUpdateMachineVo,machine);
        if(allMachineService.updateById(machine)){
            return Result.getSuccess().setMsg("保存编辑");
        }else
            return Result.getFailure().setMsg("保存失败");
    }

    //新增接口
    @PostMapping("/save")
    public Result save(@RequestBody RequestSaveMachineVo requestSaveMachineVo){
        SysUser sysUser=userService.getById(requestSaveMachineVo.getUserId());
        if(Objects.equals(sysUser.getType(), "2")){
            return Result.getFailure().setMsg("权限不足!");
        }
        if(allMachineService.queryByNum(requestSaveMachineVo.getMachineNum(),requestSaveMachineVo.getMachineModel())){
            return Result.getFailure().setMsg("编号重复!");
        }
        AllMachine machine=new AllMachine();
        BeanUtils.copyProperties(requestSaveMachineVo,machine);
        machine.setMachineInfoId(requestSaveMachineVo.getMachineModel());
        machine.setDeptId(requestSaveMachineVo.getDeptId());
        if(allMachineService.save(machine)) {
            return Result.getSuccess().setMsg("新增成功!");
        }else
            return Result.getFailure().setMsg("新增失败.");
    }
}
